<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
//$_COOKIE['PHPSESSID'] = $_POST['sessionid'];
//session_id($_COOKIE['PHPSESSID']);
//echo var_dump($_POST['sessionid']);
//define('IN_ECS', true);
//require('../includes/init.php');
include_once('comm.php');
include_once('fm/comm.php');
$myt = new thunder();

$FMUID = $_POST['userid'];
$goods_id = $_POST['gid'];
//$FMUID = 0;

//echo var_dump($_REQUEST);
//echo var_dump($_FILES);
//echo var_dump($_SESSION);

if(isset($FMUID)){
    $memo = '';
    $dirpath = $fm_rootdir.'/uploads/maked/'.$FMUID.'/images/';
    $thumbpath = $fm_rootdir.'/uploads/maked/'.$FMUID.'/thumbs/';
    $dir2 = '/uploads/maked/'.$FMUID.'/images/';
    $dirthumb = '/uploads/maked/'.$FMUID.'/thumbs/';
	$memo = '';

	$dirpath = $fm_rootdir.'/uploads/maked/'.$FMUID.'/images/';
	$thumbpath = $fm_rootdir.'/uploads/maked/'.$FMUID.'/thumbs/';
	$dir2 = '/uploads/maked/'.$FMUID.'/images/';
    
	$dir11 = '/uploads/maked/'.$FMUID.'/';
	$dir22 = '..'.$dir11;
	if(!is_dir($dir22)){
		mkdir($dir22);
		echo 'dir22';
	}
	
    if(!is_dir($dirpath)){
        mkdir($dirpath);
    }
    if(!is_dir($thumbpath)){
        mkdir($thumbpath);
    }
    
    $filename = '';
    $d = getimagesize($_FILES['Filedata']['tmp_name']);
    switch($d['mime']){
        case 'image/png' :
            $filename = '.png';
            break;
        case 'image/jpeg' :
            $filename = '.jpg';
            break;
        default :
            $filename = substr($_FILES['Filedata']['name'], -4, 4);
            break;
    }
    $filename = time().$filename;
    $uploadpath = $dirpath.$filename;//上传文件的存放路径
    $re = move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadpath);
    
    
    
    //�������ͼ
    //ȡͼƬ��ʵ���
    $img = NewMagickWand();
    MagickReadImage ( $img, $uploadpath );
    $h = MagickGetImageHeight($img);
    $w = MagickGetImageWidth($img);
        
    $thumbfile = $fm_rootdir.$dirthumb.$filename;
    if($h>80 || $w>80){
        $width = 80;
        $height = 80;
        $xratio = $width/$w;
        $yratio = $height/$h;
        
        if($h < $w) {
            $height = floor($h*$xratio);
        }else{
            $width = floor($w*$yratio);
        }
        
        //ȥ����ɫ���õ�ע����Ϣ
        MagickRemoveImageProfiles($img);
        //ʹ��PointFilter��ʽ���ŵ�5����Ŀ���С
        //MagickSampleImage($img,$width,$height);
        //Resize��Ŀ��ߴ�,ͬʱ�񻯴���
        MagickResizeImage($img,$width,$height,MW_PointFilter,0.45);
        MagickSetImageFormat($img,'image/png'); //�����ļ�����
        $ok = MagickWriteImage($img,$thumbfile);
        DestroyMagickWand($img);
        if(!$ok){
            echo 'make thumb false';
            exit; 
        }
        
    }else{
        //��������ͼ
        if(copy($uploadpath, $thumbfile)){
            $result = "saved";
        }else{
            $result = 'copy file failure';
            exit;
        }
    }
    
    
    $filename2 = $dir2.$filename;
    $filenamethumb = $dirthumb.$filename;
    $size = $d[0].'*'.$d[1];
    //$inquery = "INSERT INTO `fmphoto`(uid,url,width,height,memo,thumb)VALUES('$FMUID','$filename2','$d[0]','$d[1]','$memo','$filenamethumb')";
    $query = "INSERT INTO `fmmaked`(uid,url,memo,width,height,aid,thumb,size,goods_id)VALUES('$FMUID','$filename2','$memo','$d[0]','$d[1]', '0', '$filenamethumb', '$size', '$goods_id')";
    $re = $myt->query($query);
    
    if($re){
        echo 'OK';
        //echo var_dump($_SESSION);
    }else{
        echo 'FALSE';
    }
}

?>